﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;



namespace SyncServer
{
    public class MyWriter
    {
        public System.IO.MemoryStream answer = null;
        //private ICSharpCode.SharpZipLib.GZip.GZipOutputStream stream = null;
        
        private System.IO.Stream stream = null;
        public MyWriter(System.IO.MemoryStream answer)  
	    {
            try
            {
                stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(answer);
                this.answer = answer;
            }
            catch { }
		    
            
	    }

        public MyWriter(System.IO.MemoryStream answer, bool EnabledGzip)
        {
            if(EnabledGzip)
               stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(answer);
            else
                stream = answer;
            this.answer = answer;
           


        }





      
	
	    public void Write(String towrite)
	    {
            try
            {
               
                byte[] data = System.Text.Encoding.UTF8.GetBytes(towrite);
                stream.Write(data, 0, data.Length);
                
            }
            catch{}
	    }



        public void Println(String toWrite)
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(toWrite + "\n");
            stream.Write(data, 0, data.Length);
        }

        public void Finish()
        {
            try
            {
                if (stream is ICSharpCode.SharpZipLib.GZip.GZipOutputStream)
                    ((ICSharpCode.SharpZipLib.GZip.GZipOutputStream)stream).Finish();
                else
                    stream.Flush();
            }
            catch
            {

            }
        }
	    public void Close() 
	    {
            try
            {
               stream.Close();
               	
            }catch{}
	    }
    	
	    public void Flush()
	    {
            try
            {
                stream.Flush();
                
            }
            catch { }
	    }
    }
}
